//
//  ABSGoodsDetailHeaerReusableView.m
//  商品详情
//
//  Created by 我演示 on 2019/10/24.
//  Copyright © 2019 Jerry-Fu. All rights reserved.
//

#import "ABSGoodsDetailHeaerReusableView.h"
#import "ABSTableviewHeaderView.h"
#import "DetailRootBannerModel.h"
#import "ABSTableviewHeaderView.h"
#import "DetaiDescribeStoreTableViewCell.h"
#import "DetailShowImageViewCell.h"

@interface ABSGoodsDetailHeaerReusableView ()<UITableViewDelegate,UITableViewDataSource,SDCycleScrollViewDelegate>

/**label*/
@property(nonatomic,weak) UILabel *nameLabel ;


/**tableView*/
@property(nonatomic,strong)  UITableView *tableView;



@end


@implementation ABSGoodsDetailHeaerReusableView

//1:懒加载控件
-(UITableView *)tableView {
    
    if (!_tableView ) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) style:UITableViewStylePlain];
        
        _tableView.dataSource = self ;
        _tableView.delegate = self ;
    
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO ;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone ;
        
        _tableView.scrollEnabled = NO;
        
    }
    return _tableView;
    
    
}


-(instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self mackUpUI];
    }
    return self;
}

-(void)mackUpUI {
    [self addSubview:self.tableView];
}

/// 赋值操作
-(void)setModel:(DetailRootMvvmModel *)model {
    _model = model ;
}

#pragma mark -- 设置对应的tableVIew的控件信息 和protion有关

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    if (self.model.rootModel.promotion == 0) {
        return 5 ;
    }else {
        return 6;
    }
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 1 ;
    } else if (section == 1) {
        return 1;
    }
    if (self.model.rootModel.promotion == 0) {
        if (section == 3) {
            return 1;
        }else if (section == 4) {
            return self.model.rootModel.detailsImgs.count;
        }
        
    }

    
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *string = @"string";
    
    DetailRootModel *model = self.model.rootModel;
        
    ///商品详细描述
    if (indexPath.section == 0  ) {
        DetailDescribeFirstCell *cell = [tableView dequeueReusableCellWithIdentifier:string];
       
        if (cell == nil) {
            cell = [[DetailDescribeFirstCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:string];
        }
        cell.model =  model;
  
        return cell ;
        
    } else if (indexPath.section == 1) {
        /// 选择规格尺码
        DetailGoodsSpecificationCell *cell = [tableView dequeueReusableCellWithIdentifier:string];
        
        if (cell == nil) {
            cell = [[DetailGoodsSpecificationCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:string];
        }
        return cell;
    } else if (indexPath.section == 2 ) {
        
        /// 正品保证的图片信息
        DetailGoodsShowSelectCell *cell = [tableView dequeueReusableCellWithIdentifier:string];
        if (cell == nil) {
            cell = [[DetailGoodsShowSelectCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:string];
        }
        cell.backgroundColor = UIColor.whiteColor;
        return  cell;
    } else if (self.model.rootModel.promotion == 0) {
        if (indexPath.section  == 3 ) {
        /// 店铺的cell ，内嵌collectionView
             DetaiDescribeStoreTableViewCell *cell = [DetaiDescribeStoreTableViewCell cellWIthTableView:tableView];
             cell.backgroundColor = UIColor.grayColor;
             return cell;
        } else if(indexPath.section == 4) {
//            DetaiDescribeStoreTableViewCell
            DetailShowImageViewCell *cell = [DetailShowImageViewCell cellWithTableView:tableView];
                      cell.model = self.model.rootModel.detailsImgs[indexPath.row];
                      return  cell;
        }
        
   
        
    }
    
  
    
//    DetailShowImageViewCell *cell = [DetailShowImageViewCell cellWithTableView:tableView];
//            cell.model = self.model.rootModel.detailsImgs[indexPath.row];
//            return  cell;

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:string];
    if (cell  == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:string];
    }
    cell.textLabel.text = @"等待替换";
    return cell;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    if (section == 0) {
        return self.model.bannerHeight ;
    }

    return  0.5;
}

/// 如何根据显示数据，随时插入对应的数据信息，。并且防止越界

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return self.model.showDetailHeight;
    }
    
    /// 如果具有参加活动
    if (self.model.rootModel.promotion == 0) {
        if (indexPath.section == 3) {
              return self.model.detailImageHeight;
        }
        if (indexPath.section == 4) {
            return self.model.detailImageHeight;
        }
        
        
    } else {
        if (indexPath.section == 4) {
            
        }
    }
    
    return 120;
    
}



-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    static NSString *headerIdenty = @"header";
    
    if (section == 0) {
        ABSTableviewHeaderView *headerView =  [tableView dequeueReusableHeaderFooterViewWithIdentifier:headerIdenty];
        
        if (headerView == nil) {
            headerView =[[ ABSTableviewHeaderView alloc] initWithReuseIdentifier:headerIdenty];
        }
        
        headerView.imageModel = self.model.rootModel ;

        return headerView;
    }
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = [UIColor grayColor];
    view.alpha = 0.2;
    return view;
   
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

@end
